package cn.stronglink.assetmanage.business.setting.synccontent;


import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.SyncContent;

/**
 * 同步内容设置
 * A simple {@link Fragment} subclass.
 */
public class SystemSyncContentFragment extends Fragment implements SyncListener {
    private Unbinder mUnbinder;
    @BindView(R.id.recycleSynContent)
    RecyclerView mRecyclerView;
    private SystemSyncContentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system_sync_content, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        adapter.notifyDataSetChanged();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new SystemSyncContentAdapter(getContext());
        adapter.setOnItemClickListener(this);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        //top间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.TOP_DECORATION, 25);
        //底部间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION, 25);
        //左间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.LEFT_DECORATION, 11);
        //右间距
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 11);

        mRecyclerView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public void onSynItemClick(View view, int postion) {
        if (!adapter.list.get(postion).getIsSelected()) {
            SyncContent entity = adapter.list.get(postion);
            entity.setIsSelected(true);
            DaoManager.getDaoSession(getContext()).getSyncContentDao().update(entity);
        } else {
            SyncContent entity = adapter.list.get(postion);
            entity.setIsSelected(false);
            DaoManager.getDaoSession(getContext()).getSyncContentDao().update(entity);
        }
        adapter.notifyDataSetChanged();
    }

}
